var orgController = OrgController();
var monitorPointController = MonitorPointController();
var alarmController = AlarmController();
var monitorPointId = '1';//左侧默认的监测点id，设定默认值
var childRealTimeFlag = 0;//实时监控子页面的标志
var rightMenuFlag = 'realTime.html';//右侧导航的标志，默认设置为实时监控的标志；


function CommonService() {
    var service = {};
    service.getChildren = function (parentId) {
        orgController.getChildren(parentId,function (resp) {
            //console.log(resp);
        });
    };

    //
    service.getAllOrg = function () {
        orgController.getAllOrg(function (resp) {
            //console.log(resp);
           var arr = [];
           resp.data.map(function (item) {
                var temp = {};
                temp.id = item.id;
                temp.pId = item.parentId;
                temp.name = item.name;
                temp.open = true;
                arr.push(temp);
           });

        });
    };

    //获取所有组织点和监控点
    service.getMonitorPointTree = function () {
        monitorPointController.getMonitorPointTree(function (resp) {
            var data = resp.data;
            var nodes = [];
            factorial(data,nodes);
            console.log(nodes);
            var setting = {
                view: {
                    showIcon: false,
                    nameIsHTML: true,
                    fontCss : {
                        fontSize:{"font-size":'15px'}
                    },
                     showLine: false
                },
                data: {
                    simpleData: {
                        enable: true
                    }
                },
                callback: {
                    onClick: function (event, treeId, treeNode, clickFlag) {
                        var treeObj = $.fn.zTree.getZTreeObj(treeId);
                        var node = treeObj.getNodeByParam("id", treeNode.pId, null);
                        //判断当前页面是否在二级子页面中
                        //若flag为1，表示子页面在二级界面中
                        if(childRealTimeFlag){
                            //修正相关css的样式
                            $(".center").css('width','66%');
                            $(".right").css('width','14%');
                            $(".right").show();
                            var iframe = $('#center-main')[0].contentWindow;
                            iframe.$(".main").css('display', 'block');
                            iframe.$(".diagram").css('display', 'none');
                            iframe.$(".chart").css('display', 'flex');
                            iframe.$(".sheet").css('display', 'none');
                            $("#back").attr('hidden', 'hidden');
                            if(treeNode.name === '污水监测'){
                                $("#center-menu-title").html(node.name+'&nbsp;>>污水监测');
                                $("#center-main").attr('src',rightMenuFlag);
                                service.getMonitorPointId();
                            }else if(treeNode.name === '大气监测'){
                                $("#center-menu-title").html(node.name+'&nbsp;>>大气监测');
                                $("#center-main").attr('src',rightMenuFlag);
                                service.getMonitorPointId();
                            }
                            childRealTimeFlag = 0;
                        }else{
                            if(treeNode.name === '污水监测'){
                                $("#center-menu-title").html(node.name+'&nbsp;>>污水监测');
                                $("#center-main").attr('src',rightMenuFlag);
                                service.getMonitorPointId();
                            }else if(treeNode.name === '大气监测'){
                                $("#center-menu-title").html(node.name+'&nbsp;>>大气监测');
                                $("#center-main").attr('src',rightMenuFlag);
                                service.getMonitorPointId();
                            }
                        }
                        //加载异常信息的数量
                        service.getAlarmCount();

                    }
                }
            };
            $.fn.zTree.init($("#treeDemo"), setting, nodes);


            var f = '21';//所选的ID   例如 "12589"
            var treeObj = $.fn.zTree.getZTreeObj("treeDemo");//ztree树的ID
            var node = treeObj.getNodeByParam("id", f);//根据ID找到该节点
            treeObj.selectNode(node);//根据该节点选中
        })
    };

    //设定默认监测点公共id
    service.getMonitorPointId = function () {
        var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
        var nodes = treeObj.getSelectedNodes();
        if(nodes.length > 0){
            monitorPointId = nodes[0].selfId;
        }

    };

    //获取异常信息的条数
    service.getAlarmCount = function () {
        alarmController.getUncheckedAlarmCount(monitorPointId,common.dateFormatter_inverse(service.getTodayTime().start),common.dateFormatter_inverse(service.getTodayTime().end),function (resp) {
            if(resp.data >= 1 ){
                $("#abnormalNum").css('visibility', 'visible');
                $("#abnormalNum").html(resp.data);
            }else{
                $("#abnormalNum").css('visibility', 'hidden');
                $("#abnormalNum").html(resp.data);
            }
        })
    };
    //获取当天的时间
    service.getTodayTime = function () {
        var start = new Date(new Date(new Date().toLocaleDateString()).getTime());
        var end = new Date(new Date(new Date().toLocaleDateString()).getTime()+24*60*60*1000-1);
        return {start:start,end:end};
    };

    return service;
}

var service  = CommonService();

//递归方法生成左侧的组织导航树
function factorial(node,arr) {

    var temp = {};
    temp.id = node.currentOrg.id;
    temp.name = node.currentOrg.name;
    temp.pId = node.currentOrg.parentId;
    temp.open = true;
    arr.push(temp);

    if(node.monitorPoints.length != 0){
        node.monitorPoints.map(function (item) {
            var t = {};
            t.id = temp.id.concat(item.id);
            t.selfId = item.id;
            t.name = item.name;
            t.pId = temp.id;
            if(item.name === '污水监测'){
               t.checked = true;
            }
            arr.push(t);
        })
    }

    if(node.children.length === 0 ){
         return;
    }
    else{
        node.children.map(function (item) {
            factorial(item,arr);
        });
    }
}

function init(){
    $("#tip").hide();
    //右侧菜单导航
    $(".content .right .right-menu li").click(function(){
        $('.content .right .right-menu li').css("background-color","#072c53");
        $(this).css("background-color","#4a90e2");
        $("#right-menu").data('id',this.id);
    });
    //实时监控
    $("#realTimeMenu").on('click', function () {
        $("#center-main").attr('src','realTime.html');
        service.getMonitorPointId();
        rightMenuFlag = "realTime.html";
    });
    //监测数据
    $("#monitorMenu").on('click', function () {
        $("#center-main").attr('src','monitor.html');
        service.getMonitorPointId();
        rightMenuFlag = "monitor.html";
    });
    //异常信息
    $("#abnormalMenu").on('click', function () {
        $("#center-main").attr('src','abnormal.html');
        service.getMonitorPointId();
        rightMenuFlag = "abnormal.html";
    });
    //数据状态
    $("#stateMenu").on('click', function () {
        $("#center-main").attr('src','state.html');
        service.getMonitorPointId();
        rightMenuFlag = "state.html";
    });
    //基本信息
    $("#essentialMenu").on('click', function () {
        $("#center-main").attr('src','essential.html');
        service.getMonitorPointId();
        rightMenuFlag = "essential.html";
    });
    //树的搜索查询
    $("#keyTreeSearchBtn").on('click', function () {
        var key = $("#keyTreeSearch").val();
        var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
        var nodes = treeObj.getNodesByParamFuzzy("name", key, null);
        console.log(nodes);
        if(nodes.length > 0){
            nodes.map(function (node) {
                treeObj.selectNode(node);
            });
        }
    });
    //返回上一节
    $("#back").on('click', function () {
        $(".center").css('width','66%');
        $(".right").css('width','14%');
        $(".right").show();
        var iframe = $('#center-main')[0].contentWindow;
        iframe.$(".main").css('display', 'block');
        iframe.$(".diagram").css('display', 'none');
        iframe.$(".chart").css('display', 'flex');
        iframe.$(".sheet").css('display', 'none');
        $("#back").attr('hidden', true);
        if(parent.monitorPointId === '2'){
            setInterval(function () {
                iframe.realTimeGasService.load(parent.monitorPointId);
            },1000);
        }
    });
    //关闭告警窗口
    $("#tipClose").on('click', function () {
        $("#tip").hide();
    });
    //获取所有的组织点
    service.getAllOrg();
    //获取左侧的导航树
    service.getMonitorPointTree();
    //加载异常信息的数量
    service.getAlarmCount();
    //间隔函数加载
    setInterval(function () {
        //加载异常信息的数量
        service.getAlarmCount();
    },10000);


    //header树导航的相关跳转
    $("#zhzx").on('click', function () {
        window.location.href = vuehref+'Zhzx';
    });
    $("#aqsc").on('click', function () {
        window.location.href = vuehref+'Wxjg';
    });
    $("#aqbw").on('click', function () {
        window.location.href = vuehref+'Bmgl';
    });
    $("#xfaq").on('click', function () {
        window.location.href = vuehref+'Xfaq';
    });
    $("#spzx").on('click', function () {
        window.location.href = vuehref+'Zhyp';
    });
    $("#tjfx").on('click', function () {
        window.location.href = vuehref+'Kpfx';
    });
    $("#dssq").on('click', function () {
        window.location.href = vuehref+'Dian';
    });
    $("#tcxt").on('click', function () {
        layer.confirm('是否退出系统？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            window.location.href = vuehref;
        }, function(){

        });

    });
}





$(document).ready(function(){

    init();

});
